<template>
  <div>
    <el-row class="row-bg" type="flex" align="middle">
      用户管理模块为你展示当前系统中的用户信息，同时，在本模块你还可以新增/编辑用户信息。
    </el-row>
    <div>
      <el-card>
        <ft-pagination :current-page="1" :total="totalRowNum" @pageChange="loadDataDept">
          <div slot="toolbar" style="text-align: left;display:inline-block;">
            <el-button size="small" type="primary" icon="el-icon-refresh" @click="loadDataDept(pageIndex,pageSize)">刷新
            </el-button>
            <el-button size="small" type="primary" icon="el-icon-plus" :disabled="!orgIds"
                       @click="add">新建
            </el-button>
            <el-input style="width: 240px;margin-left: 72px;" type="text" v-model="filter" placeholder="请输入..."
                      @change="loadDataDept(pageIndex, pageSize)"></el-input>
            <el-cascader
              style="width: 250px;"
              placeholder="请选择机构"
              :options="dataOrg"
              v-model="orgIds"
              :props="orgProps"
              :change-on-select="true"
              :show-all-levels="false"
              @change="handleChange">
            </el-cascader>
          </div>
          <el-row v-if="dataDept.length==0" style="text-align: center;color: #DD8800;font-size: 16px;">
            当前系统中还没有任何用户信息，点击上方 “新建” 按钮添加相关信息！
          </el-row>
          <el-table :data="dataDept" v-if="dataDept.length>0" v-loading="loading">
            <el-table-column label="编码" prop="userCode" align="left"/>
            <el-table-column prop="userName" label="姓名" align="left"/>
            <el-table-column prop="sex" label="性别" align="left">
              <template slot-scope="scope">
                <span v-if="scope.row.sex=='MALE'">男</span>
                <span v-if="scope.row.sex=='FEMALE'">女</span>
              </template>
            </el-table-column>
            <el-table-column prop="mobilePhone" label="手机" align="left"/>
            <el-table-column prop="mail" label="邮箱" align="left"/>
            <el-table-column prop="officePhone" label="办公电话" align="left"/>
            <el-table-column label="功能操作" align="center">
              <template slot-scope="scope">
                <el-button slot="reference" type="text" size="small"
                           @click="edit(scope.$index, scope.row)">编辑
                </el-button>
                <el-button slot="reference" type="text"
                           v-if="(scope.row.status=='INIT'||scope.row.status=='STOP')" size="small"
                           @click="enabled(scope.$index, scope.row)">启用
                </el-button>
                <el-button slot="reference" type="text" v-if="(scope.row.status=='START')"
                           size="small"
                           @click="disabled(scope.$index, scope.row)">禁用
                </el-button>
                <el-popover
                  v-if="scope.row.status=='INIT'"
                  placement="top"
                  width="160"
                  v-model="scope.row.delVisible">
                  <p>确定删除吗？</p>
                  <div style="text-align: right; margin: 0">
                    <el-button size="small" type="text" @click="scope.row.delVisible = false">取消</el-button>
                    <el-button type="primary" size="small" @click="handleDelete(scope.$index, scope.row)">确定</el-button>
                  </div>
                  <el-button slot="reference" type="text" size="small" style="color: #b96463">删除</el-button>
                </el-popover>
              </template>
            </el-table-column>
          </el-table>
        </ft-pagination>
      </el-card>
    </div>
  </div>
</template>

<script>
  export default {
    name: "UserList",
    props: ['tempOrgId'],
    mounted() {
      this.loadOrg()
    },
    data() {
      return {
        dataDept: [],
        dataOrgDept: [],
        pageIndex: 1,
        pageSize: 8,
        totalRowNum: 0,
        filter: null,
        orgIds: this.tempOrgId,
        firstQuery: true,
        loading: false,
        dataOrg: [],
        orgProps: {
          children: 'children',
          value: 'orgId',
          label: 'orgName'
        }
      }
    },
    methods: {
      loadDataDept(pageIndex, pageSize) {
        this.loading = true;
        this.pageIndex = pageIndex
        this.pageSize = pageSize
        this.dataDept = []
        if (this.orgIds) {
          this.$http.post('/joys-rest/joys-master-data/users/queryOrg/' + this.orgIds[0], this.filter, {
            params: {
              pageIndex: pageIndex,
              pageSize: pageSize
            }
          }).then(resp => {
            this.dataDept = resp.body.rows
            this.totalRowNum = resp.body.total
            this.firstQuery = false
            this.loading = false;
          }, resp => {
            this.$message.error(resp.body.message)
            this.loading = false;
          })
        }
      },
      loadOrg() {
        this.$http.get('/joys-rest/joys-master-data/orgs/query/tenant').then(resp => {
          this.dataOrg = resp.body
          this.dataOrg.forEach((item) => {
            this.orgChildren(item)
          })
        }, resp => {
          this.$message.error(resp.body.message)
        })
      },
      orgChildren(value) {
        if (value.children.length > 0) {
          value.children.forEach((item) => {
            this.orgChildren(item)
          })
        } else {
          value.children = null
        }
      },
      edit(index, value) {
        this.$emit("returnValue", {
          status: 'user-detail',
          userId: value.userId,
          orgId: this.orgIds[0],
          editable: true,
          deptId: null
        });
      },
      handleChange(value) {
        if (value && value.length > 0) {
          this.loadDataDept(this.pageIndex, this.pageSize)
        }
      },
      visibleChange(value) {
        alert(value)
      },
      handleDelete(index, value) {
        this.$http.delete('/joys-rest/joys-master-data/users/' + value.userId + "/" + value.dataVersion).then(resp => {
          this.dataDept.splice(index, 1)
          this.$message.success('删除成功！')
        }, resp => {
          this.$message.error(resp.body.message)
        })
      },
      add() {
        this.$emit("returnValue", {status: 'user-detail', editable: true, orgId: this.orgIds[0]});
      },
      enabled(index, value) {
        value.status = 'START'
        this.$http.post('/joys-rest/joys-master-data/users/saveUser', value).then(resp => {
          this.$set(this.dataDept, index, resp.body)
          this.$message.success('启用成功！')
        }, resp => {
          this.$message.error(resp.body.message)
        })
      },
      disabled(index, value) {
        value.status = 'STOP'
        this.$http.post('/joys-rest/joys-master-data/users/saveUser', value).then(resp => {
          this.$set(this.dataDept, index, resp.body)
          this.$message.success('禁用成功！')
        }, resp => {
          this.$message.error(resp.body.message)
        })
      },
      lableEllipsis(value) {
        if (value.length > 12) {
          return value.substr(0, 12)+'......'
        }
        return value
      }
    }
  }
</script>

<style scoped>

</style>
